package com.ynunicom.dc.dingdingcontractapp.controller;

import com.alibaba.fastjson.JSONObject;
import com.ynunicom.dc.dingdingcontractapp.dto.ResponseDto;
import com.ynunicom.dc.dingdingcontractapp.dto.exception.TokenNullException;
import com.ynunicom.dc.dingdingcontractapp.entity.requestbodyentity.GetUserIdRequestBodyEntity;
import com.ynunicom.dc.dingdingcontractapp.entity.StandReturnerImpl;
import com.ynunicom.dc.dingdingcontractapp.entity.requestbodyentity.TransBody;
import com.ynunicom.dc.dingdingcontractapp.service.GetAccessTokenService;
import com.ynunicom.dc.dingdingcontractapp.service.GetUserIdService;
import com.ynunicom.dc.dingdingcontractapp.service.impl.GetUserIdServiceImpl;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author: jinye.Bai
 * @date: 2020/5/22 15:39
 */


@RestController
@RequestMapping("/auth")
@Slf4j
public class AccessTokenController {

    @Resource
    GetAccessTokenService getAccessTokenService;

    @Resource
    GetUserIdService getUserIdService;

    @GetMapping("/getAccessToken")
    public ResponseDto<JSONObject> getAccessToken() throws TokenNullException {
        JSONObject returner = getAccessTokenService.get();
        return Integer.parseInt(returner.getString("errcode"))!=0? ResponseDto.failed("请求失败",returner): ResponseDto.success(returner);
    }

/**
 * 用免登code获取用户userid
 */
    @SneakyThrows
    @PostMapping("/getUserId")
    public ResponseDto<JSONObject> getUserId(@Validated @RequestBody GetUserIdRequestBodyEntity entity,@RequestParam("access_token") String access_token){
        TransBody<GetUserIdRequestBodyEntity> transBody = new TransBody<>(entity,access_token);
        return new StandReturnerImpl().doReturn(transBody,getUserIdService.getClass(),getUserIdService.getClass().getMethod("get",TransBody.class));
    }

}
